<template>
  <div>
    <div class="shoppingCarDiv">
      <product-type :font-col="false"/>
    </div>
    <div class="shoppingCarDivOne">
      <div v-if="!isNull" class="shoppingCarDivTwo">
        <div class="shoppingCarDivThree">
          <div class="shoppingCarDivFour">
            <span class="shoppingCarDivFive">购物车</span>
          </div>
          <div>
            <shopping-pro @selectemit="selectEmit()" @updatenumemit="updateNumEmit($event)" @ifclear="ifClear()"/>
          </div>
          <div class="shoppingCarDivSix">
            <!--付款层-->
            <div class="shoppingCarDivSeven" style="">
              <div class="continueBuy">
                <div class="continueBuyFont" @click="clickContinueBuy()">
                  <i class="el-icon-shopping-cart-2"></i>
                  <span class="shoppingCarDivEight">
                  继续选购
                </span>
                </div>
              </div>
              <div class="shoppingCarDivNine">
                <div>
                  <span class="shoppingCarDivTen">应付:</span>&nbsp;
                  <span style="font-size: 30px;">
                    ￥{{proTotalPrice}}
                  </span>
                </div>
                <div class="shoppingCarDivEleven">
                  <submit-button text-msg="去结算" @click="clickCloseShoppingCar()" :button-back-color="buttonBackColor" :button-font-color="fontColor"/>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
      <!--以下是空购物车页面-->
      <div v-if="isNull" class="shoppingCarDivTwelve">
        <div>
          <img src="https://dsfs.oppo.com/oppo/images/shopping/shp-empty-45cde114bb.png"
               class="shoppingCarDivThirteen"/>
        </div>
        <div class="shoppingCarDivFourteen">
          <span>你的购物车还是空的，快去逛逛吧 。</span>
        </div>
        <div>
          <submit-button @click="clickBuyPro()" button-font-color="white" text-msg="带我去商城"
                         button-back-color="orangered"/>
        </div>
      </div>
    </div>
    <div>
      <foot-nva/>
    </div>
  </div>
</template>

<script>
  import {mapGetters} from "vuex"
  import {mapActions} from "vuex"

  import productType from "../../components/content/productclass/ProductType"

  import footNva from "../../components/content/buttomNav/footNva";

  import SubmitButton from "../../components/content/detailPage/SubmitButton";

  import ShoppingPro from "../../components/content/shoppingcarpro/ShoppingPro";
  import {requestAllComMsg,requestShoppingCartGetAllData} from "../../network/home";

  export default {
    name: "ShoppingCar",
    components: {
      ShoppingPro,
      productType,
      footNva,
      SubmitButton
    },
    methods: {
      ...mapActions({
        updateNumMethod: "ShopCarData/updateBuyNum",
        loadShopCarData: "ShopCarData/loadShopCarData",
        loadDataForOrderSubmitData: "OrderSubmitData/loadDataForOrderSubmitData"
      }),
      //点击结算购物车信息，将购物车选中商品提取到提交订单页面，将提交商品订单中的数据全都替换为购物车数据，并且刷新session
      clickCloseShoppingCar(){
        let dataForOrderSubmitData = this.getDataForOrderSubmitData;
        this.loadDataForOrderSubmitData(dataForOrderSubmitData);

        //通过一个session将提交的购物车信息保存在本地中，当订单真实提交时，清空对应的购物车信息
        let comIds = "";
        dataForOrderSubmitData.filter(item => {//这里只需要获取商品信息
          comIds += item.comId + ",";
        })
        sessionStorage.setItem("shoppingCarTempSubmitMsg",JSON.stringify(comIds.substring(0,comIds.length-1)));

        this.refreshOrderSubmitDataSessionStorageData;//调用刷新session中数据的方法
        //跳转到提交订单页面
        this.$router.push({path: "/submitOrder"});
      },
      clickBuyPro() {//点击带我去商城跳转到首页
        this.$router.push("/home")
      },
      selectEmit() {//捕捉到子组件发送过来的事件
        //调用方法核算当前选择的商品总价格
        this.proTotalPrice = this.getTotalPrice;
      },
      updateNumEmit(event) {//修改数量方法
        //调用修改数量的函数
        this.updateNumMethod(event);
        //之后在调用刷新总金额的方法
        this.selectEmit();
      },
      ifClear() {//判断商品是否已经全部删除
        this.isNull = this.getParamLengthIfNull;
        //同时在删除一个商品后需要刷新总金额
        this.selectEmit();
      },
      clickContinueBuy() {//购物车当中点击继续选购
        this.$router.push("/home")
      }
    },
    created() {
      //购物车的数据处理
      if (sessionStorage.getItem("ShopCarData")){//判断该数据是否已经在session当中存在
        //将sessionStorage中的数据赋值给vuex
        this.loadShopCarData([...JSON.parse(sessionStorage.getItem("ShopCarData"))]);
        this.ifClear();//为了确保没有商品时显示空购物车状态
      }else {
        //向后台请求数据
        requestShoppingCartGetAllData(this.getUserId)
          .then(data => {
            //调用方法将数据替换到store中
            this.loadShopCarData(data.data);
            return Promise.resolve(true);
          }).then(data => {
            //调用刷新session数据的方法
            this.refreshShopCarSessionStorageData;
            return Promise.resolve(true);
          }).then(data => {
            this.ifClear();//为了确保没有商品时显示空购物车状态
          }).catch(data =>{
            console.log(data);
          })
      }
    },
    computed: {
      ...mapGetters({
        getTotalPrice: "ShopCarData/getTotalPriceMethod",
        getParamLengthIfNull: "ShopCarData/getParamLengthIfNull",
        refreshShopCarSessionStorageData: "ShopCarData/refreshShopCarSessionStorageData",
        getUserId: "UserMsg/getUserId",
        getDataForOrderSubmitData: "ShopCarData/getDataForOrderSubmitData",
        refreshOrderSubmitDataSessionStorageData: "OrderSubmitData/refreshOrderSubmitDataSessionStorageData"
      })
    },
    watch: {
      proTotalPrice(newVal, oldVal) {//监听总金额，修改按钮颜色
        if (newVal == 0) {
          this.buttonBackColor = "#e7e7e7";
          this.fontColor = "silver";
        } else {
          this.buttonBackColor = "orangered";
          this.fontColor = "white";
        }
      }
    },
    data() {
      return {
        proTotalPrice: 0,
        isNull: false,
        buttonBackColor: "#e7e7e7",
        fontColor: "silver"
      }
    }
  }
</script>

<style scoped>
  .shoppingCarDivFourteen {
    margin-top: 30px;
    margin-bottom: 30px
  }

  .shoppingCarDivThirteen {
    width: 100px;
    height: 70px
  }

  .shoppingCarDivTwelve {
    width: 70%;
    background-color: white;
    height: 500px;
    margin-top: 55px;
    margin-bottom: 105px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center
  }

  .shoppingCarDivEleven {
    margin-top: 20px;
  }

  .shoppingCarDivTen {
    font-size: 14px;
    font-weight: 100;
  }

  .shoppingCarDivNine {
    width: 17%;
    height: 100%;
    display: flex;
    flex-direction: column;
    justify-content: center
  }

  .shoppingCarDivEight {
    font-size: 14px;
    margin-left: 5px;
    margin-bottom: 3px;
    font-weight: 100
  }

  .el-icon-shopping-cart-2 {
    font-size: 22px;
  }

  .shoppingCarDivSeven {
    width: 100%;
    height: 150px;
    display: flex;
    justify-content: space-between;
  }

  .shoppingCarDivSix {
    width: 100%;
    height: 220px;
    display: flex;
    align-items: flex-end;
    border-top: #e7e7e7 solid 1px
  }

  .shoppingCarDivFive {
    font-size: 18px
  }

  .shoppingCarDivFour {
    width: 100%;
    height: 40px;
  }

  .shoppingCarDivThree {
    width: 94%;
    margin-top: 35px;
    margin-bottom: 35px;
  }

  .shoppingCarDivTwo {
    width: 70%;
    height: 100%;
    background-color: white;
    margin-top: 55px;
    margin-bottom: 105px;
    display: flex;
    justify-content: center;
    align-items: center
  }

  .shoppingCarDivOne {
    background-color: #e7e7e7;
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100%;
  }

  .shoppingCarDiv {
    width: 100%;
    height: 90px;
  }

  .continueBuy {
    width: 30%;
    height: 100%;
  }

  .continueBuyFont {
    cursor: pointer
  }

  .continueBuyFont:hover {
    opacity: 0.5;
  }
</style>
